/****************************************************************
 *
 * Project: IMU
 * 
 ****************************************************************/

/****************************************************************
 *
 *	Module		: "header file" mySpi.h
 *  Tool		: KeilC ARM 4.22a
 *	Chip		: STM32F407VG
 * 	History		: 30/06/2012
 *	Description	: Day 12
				  + My library to Read/Write data over I2C bus
				  + Reference to header file "myI2C.c"
 *				
 *	Author		: Dang Anh Tung			
 *	Description	: www.payitforward.edu.vn
 *				
 *
 ****************************************************************/

#ifndef MYI2C_H
#define MYI2C_H
 
 /****************************************************************
 * 							IMPORT
 ****************************************************************/
#include "stm32f4xx.h"
#include "myDef.h"
#include "stm32f4xx_i2c.h"
#include "stm32f4xx_gpio.h"

/*****************************************************************
* 							DEFINE
******************************************************************/

///*-----------------------------------------------
//*	I2C1; I2C1_SCL: PB6; I2C1_SDA: PB9
//------------------------------------------------*/
//#define I2C_SOURCE      I2C1
//#define I2C_CLOCK       RCC_APB1Periph_I2C1
//#define I2C_SPEED       (uint32_t)400000        /* maximum = 400k */
//
//#define I2C_SCL_PORT    GPIOB
//#define I2C_SCL_PIN     GPIO_PinSource6 
//#define I2C_SCL_CLOCK   RCC_AHB1Periph_GPIOB 
//#define I2C_SCL_AF      GPIO_AF_I2C1  
// 
//#define I2C_SDA_PORT    GPIOB
//#define I2C_SDA_PIN     GPIO_PinSource9
//#define I2C_SDA_CLOCK   RCC_AHB1Periph_GPIOB
//#define I2C_SDA_AF      GPIO_AF_I2C1  
//
//#define DMA_I2C1_RX_STREAM		DMA1_Stream0	
//#define DMA_I2C1_RX_CHANNEL		DMA_Channel_1
//#define	DMA_I2C1_RX_PERIPH		(uint32_t)I2C1 + 0x10		/*I2C Data register address */
//#define DMA_I2C1_RX_PRIORITY	DMA_Priority_High
//#define DMA_I2C1_RX_STREAM_IRQ	DMA1_Stream0_IRQn
//
//#define DMA_I2C1_TX_STREAM		DMA1_Stream6	
//#define DMA_I2C1_TX_CHANNEL		DMA_Channel_1
//#define	DMA_I2C1_TX_PERIPH		(uint32_t)I2C1 + 0x10		/*I2C Data register address */
//#define DMA_I2C1_TX_PRIORITY	DMA_Priority_High
//#define DMA_I2C1_TX_STREAM_IRQ	DMA1_Stream6_IRQn


/*-----------------------------------------------
*	I2C2; I2C2_SCL: PB10; I2C2_SDA: PB11
-------------------------------------------------*/
#define I2C_TIMEOUT		(uint32_t)100000

#define I2C_SOURCE              I2C2
#define I2C_CLOCK               RCC_APB1Periph_I2C2
#define I2C_SPEED               (uint32_t)400000        /* maximum = 400k */

#define I2C_SCL_PORT            GPIOB
#define I2C_SCL_PIN             GPIO_Pin_10 
#define I2C_SCL_PIN_SOURCE      GPIO_PinSource10
#define I2C_SCL_AF              GPIO_AF_I2C2  
#define I2C_SCL_CLOCK           RCC_AHB1Periph_GPIOB 
 
#define I2C_SDA_PORT            GPIOB
#define I2C_SDA_PIN             GPIO_Pin_11
#define I2C_SDA_PIN_SOURCE      GPIO_PinSource11
#define I2C_SDA_AF              GPIO_AF_I2C2  
#define I2C_SDA_CLOCK           RCC_AHB1Periph_GPIOB

#define DMA_CLOCK               RCC_AHB1Periph_DMA1

#define DMA_I2C_RX_STREAM		DMA1_Stream3	
#define DMA_I2C_RX_CHANNEL		DMA_Channel_7
#define	DMA_I2C_RX_PERIPH		(uint32_t)I2C_SOURCE + 0x10		/*I2C Data register address */
#define DMA_I2C_RX_PRIORITY	    DMA_Priority_VeryHigh
#define DMA_I2C_RX_STREAM_IRQ	DMA1_Stream3_IRQn
#define DMA_I2C_RX_IRQHandler   DMA1_Stream3_IRQHandler
#define DMA_I2C_RX_TC           DMA_FLAG_TCIF3

#define DMA_I2C_TX_STREAM		DMA1_Stream7	
#define DMA_I2C_TX_CHANNEL		DMA_Channel_7
#define	DMA_I2C_TX_PERIPH		(uint32_t)I2C_SOURCE + 0x10		/*I2C Data register address */
#define DMA_I2C_TX_PRIORITY	    DMA_Priority_VeryHigh
#define DMA_I2C_TX_STREAM_IRQ	DMA1_Stream7_IRQn
#define DMA_I2C_TX_IRQHandler   DMA1_Stream7_IRQHandler
#define DMA_I2C_TX_TC           DMA_FLAG_TCIF7
 


 /****************************************************************
 * 							GLOBAL
 ****************************************************************/


 /****************************************************************
 * 						   FUNCTIONS
 ****************************************************************/


void myI2CInit(void);
void myDmaI2CInit(void);
void myDmaI2CInterruptInit(void);

//
void myDmaI2CInit(void);
void myDmaI2CConfig(uint32_t pBuffer, uint32_t BufferSize, uint32_t Direction);

//	Call this function every time out happen
uint32_t myI2CTimeOutCallback(void);

//	Read multi-bytes (numByte) continuously begin at start address in I2C slave by the NORMAL BUS(not DMA)
BOOL myI2CReadBytes(I2C_TypeDef* I2Cx, uint8_t *readBuffer, uint8_t devAddr, uint8_t startAddr, uint32_t numByte);

//	Write multi-byte (numByte) continuously begin at start address in I2C slave	by the NORMAL BUS
BOOL myI2CWriteBytes(I2C_TypeDef* I2Cx, uint8_t *writeBuffer, uint8_t devAddr, uint8_t startAddr, uint32_t numByte);

#endif /* MYI2C_H */

